package com.lamer.console.blog.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lamer.console.blog.entity.Chapter;
import com.lamer.console.blog.service.IChapterService;
import com.lamer.console.blog.vo.ChapterVO;
import com.lamer.console.common.api.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 黄诗鹏
 * @since 2021-11-24
 */
@RestController
@RequestMapping("/blog")
public class ChapterController {

    @Autowired
    private IChapterService chapterService;

    @GetMapping("/chapter/page/")
    public ApiResult<Page<ChapterVO>> getPage(@RequestParam("search") String search) {
        JSONObject jsonObject = JSON.parseObject(search);
        Integer number = jsonObject.getInteger("page_number");
        Integer size = jsonObject.getInteger("page_size");
        String articleName = jsonObject.getString("articleName");

        QueryWrapper<ChapterVO> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(articleName)) queryWrapper.like("article.article_name", articleName);

        Page<ChapterVO> page = chapterService.pageChapterVO(new Page<>(number, size), queryWrapper);
        return ApiResult.success(page);
    }

    @GetMapping("/chapter/{id}")
    public ApiResult<Chapter> getData(@PathVariable("id") int id) {
        Chapter data = chapterService.getById(id);
        return data == null ? ApiResult.failed() : ApiResult.success(data);
    }

    @PostMapping("/chapter/")
    public ApiResult<String> insertData(@RequestBody Chapter data) {
        boolean flag = chapterService.save(data);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @PutMapping("/chapter/")
    public ApiResult<String> updateData(@RequestBody Chapter data) {
        boolean flag = chapterService.updateById(data);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @DeleteMapping("/chapter/list/")
    public ApiResult<String> deleteData(@RequestParam("ids") int[] ids) {
        List<Integer> collect = Arrays.stream(ids).boxed().collect(Collectors.toList());
        boolean flag = chapterService.removeByIds(collect);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @GetMapping("/chapter/list/{bookId}")
    public ApiResult<List<Chapter>> getChapterListByBookId(@PathVariable("bookId") int bookId) {
        LambdaQueryWrapper<Chapter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Chapter::getBookId, bookId);

        List<Chapter> list = chapterService.list(queryWrapper);
        return ApiResult.success(list);
    }

}

